<?php

/*
 * This is my first OOP project. This is a test project!
 * The name of project is Bookstore. 
 */

/**
 * This class is handling information about Authors.
 * @author Velislav Belev
 * @license   http://opensource.org/licenses/gpl-3.0.html GNU Public License
 */
namespace Models;
class Authors extends \Models\User { 
    private $_tableName;
    private $_columnName;
    private $_order;
    
    /**
     * By this method I validate author ID, check is it exists in the DB. 
     * If everything is OK I return all books of author 
     * if not I redirect user back to home page. 
     * @param int $authorId
     * @return array
     */
    public function allBooksOfAuthor($authorId) {
        $authorId = $this->validate($authorId, 1, 'Something wrong with this author', 121);
        $isAuthorInDb = $this->isInDb('authors', 'author_id', $authorId);
        if($isAuthorInDb != 1){
            header('Location: index.php');
        }
        $authorId = $this->escape($authorId);
        return $this->_queryResult = $this->query('SELECT * FROM books_authors as ba
            INNER JOIN books as b ON ba.book_id=b.book_id
            INNER JOIN books_authors as bba ON bba.book_id=ba.book_id
            INNER JOIN authors as a ON bba.author_id=a.author_id
            WHERE ba.author_id= '.$authorId);       
    }

  
/**
 * By this method I validate author name, check is it exists in the DB. 
 * If everything is OK I save new author in the DB if not it throws exceptions. 
 * @param int $isAdmin - 1 is for registered users, 2 is for admins
 * @param str $authorName
 * @param str $exception
 * @param int $code
 * @throws \Exception
 */
    public function saveAuthor($isAdmin, $authorName, $exception, $code) {
        if($isAdmin != 2){
             throw new \Exception('Only administrator can enter new authors', 125); 
        }
        $isAuthor = $this->validate($authorName, 2, 
                'Author name must be at least 2 characters long', 120);
        $authorName =  $this->escape($authorName);        
        if(!empty($isAuthor)) {
           $queryResult = $this->isInDb('authors', 'author_name', $isAuthor);
           if(empty($queryResult)) {
                $insertData = array('author_name' => $isAuthor);
                if($this->insert('authors', $insertData)) {
                    echo 'Saved';
                };                               
            } else {
            throw new \Exception('This author name already exists in the DB', 102); 
            }
        }           
    }
    
    /**
     * By this method I set all data needed to get all authors from the DB.
     * It is used in add_book.php
     * @param str $tableName
     * @param str $columnName
     * @param str $order
     * @throws \Exception
     */
    public function setAllFromOneTable($tableName,$columnName, $order) {
       $tableName = trim($tableName);
       $tableName = $this->escape($tableName);
       $columnName = trim($columnName);
       $columnName = $this->escape($columnName);
       $order = trim($order);
       $order = $this->escape($order);       
        
        $this->_tableName=$tableName;
        $this->_columnName=$columnName;
        $this->_order=$order;
        $this->orderBy("$columnName","$order");
        $this->_queryResult = $queryResult = $this->get($tableName); 
        if(!$queryResult) {
            throw new \Exception('DB error', 103);
        }        
    }

    /**
     * By this method I get all authors from the DB. It is used in add_book.php
     * @return array
     */
    public function getAllFromOneTable() {
       return $this->_queryResult;
    }



    
    
    
    
    
    
    
    
    
    
    
    
}

